package com.iaz.tech.tools.property.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

/**
 * 属性工具
 */
public class PropertyUtil {
    /**
     * 将byte 数据转换成缓冲区
     * @param _byte_value
     * @return
     */
    public static byte[] byteToBuffer(byte _byte_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeByte(_byte_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    /**
     * 将字符流转换成byte
     * @param _buffer
     * @return
     */
    public static byte bufferToByte(byte[] _buffer) {
        byte result = 0;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readByte();

        }catch(Exception e){
            result = 0;
        }
        return result;
    }

    /**
     * char 转换成byte 缓冲区
     * @param _ch_value
     * @return
     */
    public static byte[] charToBuffer(char _ch_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeChar(_ch_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    public static char bufferToChar(byte[] _buffer) {
        char result = 0;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readChar();

        }catch(Exception e){
            result = 0;
        }
        return result;
    }

    /**
     * 将Boolean 写入到byte[] 字节流
     * @param _value
     * @return
     */
    public static byte[] booleanToBuffer(boolean _value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeBoolean(_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    public static boolean bufferToBoolean(byte[] _buffer) {
        boolean result = false;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readBoolean();

        }catch(Exception e){
            result = false;
        }
        return result;
    }

    /**
     * 将short 的值输出为字节流
     * @param _short_value
     * @return
     */
    public static byte[] shortToBuffer(short _short_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeShort(_short_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    public static short bufferToShort(byte[] _buffer) {
        short result = 0;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readShort();

        }catch(Exception e){
            result = 0;
        }
        return result;
    }

    /**
     * 将整形数 的值输出为字节流
     * @param _int_value
     * @return
     */
    public static byte[] integerToBuffer(int _int_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeInt(_int_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    /**
     * 字节流转换成int
     * @param _buffer
     * @return
     */
    public static int bufferToInteger(byte[] _buffer) {
        int result = 0;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readInt();

        }catch(Exception e){
            result = 0;
        }
        return result;
    }
    /**
     * 将将长整数数 的值输出为字节流
     * @param _long_value
     * @return
     */
    public static byte[] longToBuffer(int _long_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeLong(_long_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }
    public static long bufferToLong(byte[] _buffer) {
        long result = 0;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readLong();

        }catch(Exception e){
            result = 0L;
        }
        return result;
    }
    /**
     * 将float 的值输出为字节流
     * @param _float_value
     * @return
     */
    public static byte[] floatToBuffer(float _float_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeFloat(_float_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    public static float bufferToFloat(byte[] _buffer) {
        float result = 0.0f;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readFloat();

        }catch(Exception e){
            result = 0.0f;
        }
        return result;
    }
    /**
     * 将double的值输出为字节流
     * @param _double_value
     * @return
     */
    public static byte[] doubleToBuffer(double _double_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeDouble(_double_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }
    public static double bufferToDouble(byte[] _buffer) {
        double result = 0.0d;
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readDouble();

        }catch(Exception e){
            result = 0.0f;
        }
        return result;
    }
    /**
     * 将字符串用UTF-8 的模式
     * @param _string_value
     * @return
     */
    public static byte[] stringToBuffer(String  _string_value) {
        byte[] result = null;
        try{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataout = new DataOutputStream(byteArrayOutputStream);
            dataout.writeUTF(_string_value);
            result = byteArrayOutputStream.toByteArray();
            return result;
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

    /**
     * 将缓冲区转换成字符串值
     * @param _buffer
     * @return
     */
    public static String bufferToString(byte[] _buffer) {
        String result = "";
        try {
            if ( _buffer == null ){
                throw new Exception("PropertyUtil.bufferToByte 入口参数_buffer 没有指定");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_buffer);
            DataInputStream datainput = new DataInputStream(byteArrayInputStream);
            result = datainput.readUTF();

        }catch(Exception e){
            result = "";
        }
        return result;
    }

}
